package com.sky.service.user.impl;

import com.sky.mapper.user.UserDishMapper;
import com.sky.pojo.Category;
import com.sky.pojo.Setmeal;
import com.sky.service.user.UserDishService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class UserDishServiceImpl implements UserDishService {
    @Autowired
    private UserDishMapper userDishMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<Category> selectAllCategory() {
        List<Category> list = userDishMapper.selectAllCategory();
        return list;
    }
    /*public List<Category> selectAllCategoryFromCache() {
        //先从缓存中获取，有直接返回，没有 从数据库查，然后添加
        String jsonString = (String) redisTemplate.opsForValue().get("CATEGORY-LIST");
        if (StringUtils.hasText(jsonString)){
            //把json 字符串对象 解析 以集合返回。
            return  JSONObject.parseArray(jsonString,Category.class);
        }
        log.info("redis中不存在分类数据，从数据库中查询");
        List<Category> list = userDishMapper.selectAllCategory();
        redisTemplate.opsForValue().set("CATEGORY-LIST",JSONObject.toJSONString(list));
        return list;
    }*/

  /*  *//**
     * 通过id 查询分类下的菜品和口味
     *
     * @param setmeal
     * @return
     *//*
    @Override
    public List<DishVO> selectDishByCategoryId(Integer categoryId) {
        //在redis中构造key的规则
        String key = RedisConstant.CATEGORY_CACHE_PREFIX + categoryId;
        String dishJosn = (String)redisTemplate.opsForValue().get(key);
        if (StringUtils.hasText(dishJosn)){
            log.info("从缓存中查询，CatrgoryId :{}",categoryId);
            return JSONObject.parseArray(dishJosn,DishVO.class);
        }
        //List<Dish> dishList = userMapper.selectDishByCategoryId(categoryId);

        List<DishVO> list = userDishMapper.selectDishByCategoryId(categoryId);
            log.info("list有值吗？值是多少？{}",list);
        List<DishVO> dishVOList = new ArrayList<>();
        for (DishVO dishVO : list) {
            DishVO dishVO1 = new DishVO();
            BeanUtils.copyProperties(dishVO ,dishVO1);
            List<DishFlavor> flavors = userDishMapper.selectDishFlavorbyCategoryId(dishVO.getId());
            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO1);
        }

        redisTemplate.opsForValue().set(key, JSON.toJSONString(dishVOList));
        return dishVOList;
    }*/



/*    //修改或者删除时，同时也清理掉缓存中的内容
    private void cleanCache(){

        Set keys = redisTemplate.keys(RedisConstant.CATEGORY_CACHE_PREFIX+"*");
        redisTemplate.delete(keys);
    }
    //修改或者删除时，同时也清理掉缓存中的内容
    private void deleteCategoryCache(){
        //redis 的删除方法 key 就是分类的key
        redisTemplate.delete("CATEGORY_LIST");
    }*/



    @Override
    @Cacheable(value = "SetMeal",key = "#p0.categoryId")
    public List<SetmealVO> selectAllSetmeal(Setmeal setmeal) {
        List<SetmealVO> list = userDishMapper.selectSetmealVOByCategoryId(setmeal);
        log.info("还是从数据库中查了");
        return list;
    }

    @Override
    public List<DishItemVO> selectDishFromSetmeal(Long setmealId) {
        List<DishItemVO> list = userDishMapper.selectDishFromSetmeal(setmealId);
        return list;
    }

}
